// ignore_for_file: must_be_immutable

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:junyeyishu/ui/home/QGHomePage.dart';
import 'package:junyeyishu/ui/mine/QGMinePage.dart';
import 'package:junyeyishu/ui/bulletin/QGBulletinPage.dart';
import 'package:junyeyishu/ui/mine/widget/QGLogOutWidgetappupdata.dart';
import 'package:junyeyishu/ui/sale/QGSalePage.dart';
import 'utils/utils.dart';

class MainTab extends StatefulWidget {
  int index=0;
  dynamic indexa;
  MainTab({Key? key, this.indexa}) : super(key: key);

  @override
  State<MainTab> createState() => _MainTabState();
}

class _MainTabState extends State<MainTab> {
  List<BottomNavigationBarItem> items = [];

  Widget empty = Container();
  List<Widget> children = [
    const QGHomePage(),
    Container(),
    Container(),
    Container(),
    Container(),
  ];

  @override
  void initState() {
    super.initState();

    setupEasyRefresh();

    User.setup();



    eventBus.on<EventMainTabSelectIndex>().listen((obj) {
      checkContent(obj.index);
      widget.index = obj.index;
      if (mounted) {
        setState(() {});
      }
    });

    // tab 分类
    items = [
      item("首页", "home",0),
      item("公告", "bulletin",2),
      item("我的", "mine",3),
    ];

    if (!inReview()) {
      items.insert(1, item("寄售", "sale",1));
    }


 WidgetsBinding.instance.addPostFrameCallback((_) {
    if(widget.indexa['packageInfo'] < widget.indexa['number'] && !kIsWeb){
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return QGLogupapp();
      });
    }
      
    });
    
  }




  // 下拉刷新提示语
  setupEasyRefresh() {
    EasyRefresh.defaultHeaderBuilder = () => const ClassicHeader(
          dragText: "下拉刷新",
          armedText: "松开刷新",
          readyText: "加载中...",
          processingText: "加载中...",
          processedText: "刷新成功",
          messageText: "上次更新 %T",
          textStyle: TextStyle(color: Color(0xFF111111)),
          messageStyle: TextStyle(color: Color(0xFF111111)),
          iconTheme: IconThemeData(color: Color(0xFF000000)),
        );

    EasyRefresh.defaultFooterBuilder = () => const ClassicFooter(
          readyText: "加载中...",
          processingText: "加载中...",
          processedText: "加载成功",
          noMoreText: "没有更多",
          failedText: "刷新失败",
          messageText: "上次更新 %T",
          textStyle: TextStyle(color: Color(0xFF111111)),
          messageStyle: TextStyle(color: Color(0xFF111111)),
          iconTheme: IconThemeData(color: Color(0xFF000000)),
        );
  }

  void handleClickNotification(Map<String, dynamic> msg) async {}

  @override
  Widget build(BuildContext context) {

    

    return Scaffold(
      body: IndexedStack(index: widget.index, children: children),
      bottomNavigationBar: bottomBar(items),
    );
  }

  // 创建底部tabbar
  bottomBar(items) {
    return Theme(
  data: Theme.of(context).copyWith(
    splashColor: Colors.transparent,
    highlightColor: Colors.transparent,
  ),child: 
    
    BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      backgroundColor: Color(0xFF232A32),
      selectedFontSize: 12,
      unselectedFontSize: 12,
      currentIndex: widget.index,
      items: items,
      onTap: ((value) {
        if (!User.isLogin) {
          if (items[value].label == "我的") {
            Navigator.of(context).pushNamed(PageLogin);
            return;
          }
        }

         eventBus.fire(EventMainTabRefresh(index: value));

        checkContent(value);
        widget.index = value;

        setState(() {});
      }),
    )
    );
  }

  // 切换tabbar
  checkContent(index) {
    if (children[index] is Container) {
      if (inReview()) {
        switch (index) {
          case 1:
            children[index] = const QGBulletinPage();
            break;
          case 2:
            children[index] = const QGMinePage();
            break;
          default:
            break;
        }
        return;
      }

      switch (index) {
        case 1:
        
          children[index] = const QGSalePage();
          break;
        case 2:
          children[index] = const QGBulletinPage();
          break;
        case 3:
          children[index] = const QGMinePage();
          break;
      }
    }
  }

  // 每一项展示的内容
  item(title, imageName,index) {
    return BottomNavigationBarItem(
      label: '',
      activeIcon: Container(
        width: 75.rpx,
        height:38.rpx,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(19.rpx),
          color: Color(0XFF404851)
        ),
        child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
           Image.asset(
            "images/nav_$imageName.png",
            height: 17,
            gaplessPlayback: true,
            frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
        if (wasSynchronouslyLoaded || frame != null) {
          return child; // 图片已加载完成
        }
        return SizedBox(width: 10,);
      },
      errorBuilder: (context, error, stackTrace) {
        return const Icon(Icons.error); // 错误占位
      },
          ),
          SizedBox(width: 5,),
          Text(title,style: TextStyle(fontSize: 12,color: Color(0XFFFFFFFF))),
        ],
      ),
      ),
      icon: Container(
        width: 75.rpx,
        height:38.rpx,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(19.rpx),
          color: Color(0XFF232A32)
        ),
        child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
           Image.asset(
            "images/nav_$imageName.png",
            height: 17,
            gaplessPlayback: true,
            frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
        if (wasSynchronouslyLoaded || frame != null) {
          return child; // 图片已加载完成
        }
          return SizedBox(width: 10,);
      },
      errorBuilder: (context, error, stackTrace) {
        return const Icon(Icons.error); // 错误占位
      },
          ),
          SizedBox(width: 5,),
          Text(title,style: TextStyle(fontSize: 12,color: Color(0XFF232A32))),
        ],
      ),
      )
      
      
      
      
      // Column(
      //   children: [
      //     SizedBox(height: 10.rpx),
      //     Image.asset(
      //       "images/nav_$imageName.png",
      //       width: 20,
      //       gaplessPlayback: true,
      //     ),
      //     SizedBox(height: 8.rpx),
      //   ],
      // ),
    );
  }
}
